package face.pay.message.controller;

import com.github.wxpay.sdk.CheckUtil;
import com.github.wxpay.sdk.WXPayUtil;
import face.pay.message.handler.adapter.EventHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 微信公众号消息
 */
@RestController
@Slf4j
public class MessageController {

    @Autowired
    EventHandlerAdapter eventHandlerAdapter;

    /**
     * 配置时验证
     * @param signature
     * @param timestamp
     * @param nonce
     * @param echostr
     * @return
     */
    @RequestMapping(value = "/message",method = {RequestMethod.GET})
    public String message_get(String signature,String timestamp,String nonce,String echostr){

        log.info("signature ："+signature);
        log.info("timestamp ："+timestamp);
        log.info("nonce ："+nonce);
        log.info("echostr ："+echostr);

        Boolean  validate =  CheckUtil.checkSignature(signature,timestamp,nonce);
        log.info("validate："+validate);
        if(validate){
                log.info("消息验证");
                return echostr;
        }else {
            log.info("消息来源不合法");
            return null;
        }
    }

    /**
     * 微信post 消息 比如事件 对话消息
     * @param signature
     * @param timestamp
     * @param nonce
     * @param echostr
     * @param xml
     * @return
     */
    @RequestMapping(value = "/message",method = { RequestMethod.POST})
    public String message_post(String signature,String timestamp,String nonce,String echostr,@RequestBody(required = false) String xml){

        log.info("signature ："+signature);
        log.info("timestamp ："+timestamp);
        log.info("nonce ："+ nonce);
        log.info("echostr ："+echostr);

        Boolean  validate =  CheckUtil.checkSignature(signature,timestamp,nonce);
        log.info("validate："+validate);
        if(true){
            if(StringUtils.isNotBlank(echostr)){
                log.info("消息验证");
                return echostr;
            }else{
                log.info("消息接收");
                log.info(xml);

                try {
                    Map<String,String> map = WXPayUtil.xmlToMap(xml);

                    String msgType  = map.get("MsgType");
                    if("event".equals(msgType)){
                        /**
                         * 事件消息 进入事件处理中心
                         */
                        eventHandlerAdapter.doHandler(map);
                    }else if ("text".equals(msgType)){
                        /**
                         * 普通用户消息 进入用户消息中心
                         * 暂时不实现
                         */
                    }
                } catch (Exception e) {
                   log.error("解析微信post报文异常",e);
                }

                return null;
            }
        }else {
            log.info("消息来源不合法");
            return null;
        }
    }

    @RequestMapping("/hello")
    public String hello(String signature){

        log.info(signature);

        return "hello";
    }


}
